package models

import (
	"github.com/beego/beego/v2/client/orm"
	_ "github.com/go-sql-driver/mysql"
	"time"
)

type User struct {
	Id             int64     `orm:"column(id);auto;size(11)" description:"表ID" json:"id"`
	GroupId        int       `orm:"column(group_id);size(10)" description:"组别ID" json:"group_id"`
	Username       string    `orm:"column(username);size(32)" description:"用户名" json:"username"`
	Nickname       string    `orm:"column(nickname);size(50)" description:"昵称" json:"nickname"`
	Password       string    `orm:"column(password);size(32)" description:"密码" json:"password"`
	Salt           string    `orm:"column(salt);size(30)" description:"密码盐" json:"salt"`
	Email          string    `orm:"column(email);size(100)" description:"电子邮箱" json:"email"`
	Mobile         string    `orm:"column(mobile);size(11)" description:"手机号" json:"mobile"`
	Avatar         string    `orm:"column(avatar);size(255)" description:"头像" json:"avatar"`
	Level          int       `orm:"column(level);size(1)" description:"等级" json:"level"`
	Gender         int       `orm:"column(gender);size(1)" description:"性别" json:"gender"`
	Birthday       time.Time `orm:"column(birthday);type(date)" description:"生日" json:"birthday"`
	Bio            string    `orm:"column(bio);size(100)" description:"格言" json:"bio"`
	Money          float64   `orm:"column(money);digits(10);decimals(2)" description:"余额" json:"money"`
	Score          int       `orm:"column(score);size(10)" description:"积分" json:"score"`
	Successions    int       `orm:"column(successions);size(10)" description:"连续登录天数" json:"successions"`
	Maxsuccessions int       `orm:"column(maxsuccessions);size(10)" description:"最大连续登录天数" json:"maxsuccessions"`
	Prevtime       int64     `orm:"column(prevtime);size(10)" description:"上次登录时间" json:"prevtime"`
	Logintime      int64     `orm:"column(logintime);size(10)" description:"登录时间" json:"logintime"`
	Loginip        string    `orm:"column(loginip);size(50)" description:"登录IP" json:"loginip"`
	Loginfailure   int       `orm:"column(loginfailure);size(50)" description:"失败次数" json:"loginfailure"`
	Joinip         string    `orm:"column(joinip);size(50)" description:"加入IP" json:"joinip"`
	Jointime       int64     `orm:"column(jointime);size(10)" description:"加入时间" json:"jointime"`
	CreateTime     int       `orm:"column(createtime);size(10);default(0)" description:"操作时间" json:"createtime"`
	UpdateTime     int       `orm:"column(updatetime);size(10);default(0)" description:"更新时间" json:"updatetime"`
	Token          string    `orm:"column(token);size(50);default(0)" description:"Token" json:"token"`
	Status         string    `orm:"column(status);size(30);default(1)" description:"状态" json:"status"`
	Verification   string    `orm:"column(verification);;size(255);default(0)" description:"验证" json:"verification"`
}

func (*User) TableName() string {
	return "fa_user"
}

func (u *User) GetByMobile(mobile string) bool {
	o := orm.NewOrm()
	if err := o.QueryTable(u.TableName()).Filter("mobile", mobile).One(u); err != nil {
		if err == orm.ErrNoRows {
			return true
		}
	}
	return false
}

func (u *User) GetByUsername(username string) bool {
	o := orm.NewOrm()
	if err := o.QueryTable(u.TableName()).Filter("username", username).One(u); err != nil {
		if err == orm.ErrNoRows {
			return true
		}
	}
	return false
}

func (u *User) GetByNickname(nickname string) bool {
	o := orm.NewOrm()
	if err := o.QueryTable(u.TableName()).Filter("nickname", nickname).One(u); err != nil {
		if err == orm.ErrNoRows {
			return true
		}
	}
	return false
}

func (u *User) GetByEmail(email string) bool {
	o := orm.NewOrm()
	if err := o.QueryTable(u.TableName()).Filter("email", email).One(u); err != nil {
		if err == orm.ErrNoRows {
			return true
		}
	}
	return false
}

//在init中注册定义的model
func init() {
	orm.RegisterModel(new(User))
}
